package xmu.zfw.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import xmu.zfw.config.ExcelTypeConf;
import xmu.zfw.dao.ReportDao;
import xmu.zfw.domain.Report;
import xmu.zfw.domain.Reporteder;
import xmu.zfw.domain.Reporter;
import xmu.zfw.mapper.ReportMapper;
import xmu.zfw.mapper.ReportederMapper;
import xmu.zfw.mapper.ReporterMapper;
import xmu.zfw.util.ExcelExporter;
import xmu.zfw.vo.ReportVO;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;


@Component
public class ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private ReportDao reportDao;

    @Autowired
    private ReporterMapper reporterMapper;

    @Autowired
    private ReportederMapper reportederMapper;

    public List<Report> getAll(){
        List<Report> reports = reportMapper.getAll();
        reports = reportDao.getReports(reports);
        return reports;
    }

    public List<Report> getReportsByReportType(String reportType){
        List<Report> reports = reportMapper.getReportsByReportType(reportType);
        reports = reportDao.getReports(reports);
        return reports;
    }

    public List<Report> getReportsBySend(String isSend){
        List<Report> reports = reportMapper.getReportsBySend(isSend);
        reports = reportDao.getReports(reports);
        return reports;
    }

    public List<Report> getTodoReportsByIsSend(String isSend){
        List<Report> reports = reportMapper.getToDoReportsBySend(isSend);
        reports = reportDao.getReports(reports);
        return reports;
    }

    public List<Report> getReportsByProgress(String progress){
        List<Report> reports = reportMapper.getAllByProgress(progress);
        reports = reportDao.getReports(reports);
        return reports;
    }

    public List<Report> getAllTimeout(){
        List<Report> reports = reportMapper.getAllTimeout();
        reports = reportDao.getReports(reports);
        return reports;
    }

    public List<Report> getAllUrgent(){
        List<Report> reports = reportMapper.getAllUrgent();
        reports = reportDao.getReports(reports);
        return reports;
    }

    public Report getReportById(Integer id){
        return reportDao.getReportById(id);
    }
    public Report getReportByNo(String no){
        return reportDao.getReportByNo(no);
    }

    public Integer addReport(Report report){
        return reportDao.addReport(report);
    }

    public Report updateReport(Report report){
        return reportDao.updateReport(report);
    }

    public List<Report> searchReports(Report report, Date startTime, Date endTime){
        List<Report> reports = reportDao.searchReports(report, startTime, endTime);
        List<Report> outReports = new ArrayList<>();
        for(Report tmpReport: reports){
            if(report.getReporter() != null && !report.getReporter().compare(tmpReport.getReporter()) ||
                report.getReporteders().size() > 0 && !report.getReporteders().get(0).compare(tmpReport.getReporteders())){
                continue;
            }
            outReports.add(tmpReport);
        }
        return outReports;
    }

    public List<Report> searchManyReportedReports(Report report, Date startTime, Date endTime){
        List<Report> reports = reportDao.searchManyReportedReports(report, startTime, endTime);
        List<Report> outReports = new ArrayList<>();
        for(Report tmpReport: reports){
            if(report.getReporter() != null && !report.getReporter().compare(tmpReport.getReporter()) ||
                    report.getReporteders().size() > 0 && !report.getReporteders().get(0).compare(tmpReport.getReporteders())){
                continue;
            }
            outReports.add(tmpReport);
        }
        return outReports;
    }

    public Integer deleteReport(Integer id){
        return reportDao.deleteReport(id);
    }

    public Boolean importReports(List<Report> reportList){
        for(Report report: reportList){
            Report tmpRepot = reportMapper.getReportByNo(report.getNo());
            if(tmpRepot == null){
                reportDao.addReport(report);
            }else{
                report.setId(tmpRepot.getId());
                reportDao.updateReport(report);
            }
        }
        return true;
    }

    public Workbook toExcel(ExcelTypeConf.ExportType exportType) throws IOException, FileNotFoundException {
        List<Report> reportList = reportMapper.getAll();
        List<List<Object>> data = new ArrayList<>();

        for(int i=0; i < reportList.size(); i++){
            List<Object> rowData = new ArrayList<>();
            Report report = reportList.get(i);
            JSONObject json = (JSONObject) JSONObject.toJSON(report);

            rowData.add(i + 1);
            for(String attr: exportType.getAttrs()){
                rowData.add(json.get(attr));
            }
            data.add(rowData);
        }
        File template = ResourceUtils.getFile(exportType.getExcelFile());
        return ExcelExporter.toExcel(template, data);

    }
}
